package com.shanghaionstar.smart;

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.BusinessUtil;


/**
 * Created by Rengui Lu on 2016/6/12.
 */
public class SmartNoFoundDeviceFrg extends BaseFragment implements View.OnClickListener {
    @FindViewById(R.id.title_tv)
    TextView mTitleTv;
    @FindViewById(R.id.contact_phone_tv)
    TextView mPhoneTv;

    private BaseActivity mContext;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mContext = (BaseActivity) activity;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_slk_nf_device;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    private void initView() {
        mTitleTv.setText(R.string.smart_nf_device);
    }

    @Override
    @OnClick({R.id.back_ib, R.id.contact_phone_tv, R.id.reset_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_ib:
                BusinessUtil.sendReportByClickType(mContext, FunctionNameFindId.Smarthome_nofindappwifi_return.getStringValue());
                back();
                break;
            case R.id.contact_phone_tv:
                showConnectDia();
                break;
            case R.id.reset_tv:
                goToReset();
                break;
        }
    }

    private void goToReset() {
        BusinessUtil.sendReportByClickType(mContext, FunctionNameFindId.Smarthome_intro_resetappliancewifi.getStringValue());
        SmartResetFrg smartReset = new SmartResetFrg();
        mContext.changeFragment(mContext.getSupportFragmentManager(),smartReset,SmartResetFrg.class,true);
    }

    private void showConnectDia() {
        AlertDialogUtil.showConfigureDialog(mContext,
                mContext.getString(R.string.dialog_warning),
                mContext.getString(R.string.smart_contact_service_phone),
                mContext.getString(R.string.call_out),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        requestRunTimePermissions(new String[]{Manifest.permission.CALL_PHONE}, new BaseFragment.PermissionCall() {
                            @Override
                            public void requestSuccess() {
                                Intent callIntent = new Intent(Intent.ACTION_CALL,
                                        Uri.parse("tel:" + mContext.getString(R.string.connect_phone)));
                                mContext.startActivity(callIntent);
                            }

                            @Override
                            public void refused() {

                            }
                        });

                    }
                });
    }
}
